/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.dictionary;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.service.dictionary.DictionaryService;
import org.grayrabbit.cms.service.dictionary.DictionaryTypeService;
import org.grayrabbit.cms.vo.dictionary.DictionaryBean;
import org.grayrabbit.cms.vo.dictionary.DictionaryTypeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>DictionaryAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 14:46:47 
 */
@Controller
@Scope("prototype")
@Results( {
		@Result(name = "input", type = "dispatcher", location = "dictionary-input.jsp"),
		@Result(name = "success", type = "dispatcher", location = "dictionary-list.jsp")
		}
		)
public class DictionaryAction extends GenericAction<DictionaryBean> {
	private static final long serialVersionUID = -1L;
	private static final String IS_EXIST = "1";
	private static final String IS_NOT_EXIST = "0";
	//Service接口接入
	@Autowired
	@Qualifier("dictionaryServiceImpl")
	private DictionaryService dictionaryService;
	
	@Autowired
	@Qualifier("dictionaryTypeServiceImpl")
	private DictionaryTypeService dictionaryTypeService;
	
	//VO对象，由前端页面数据组装
	private DictionaryBean dictionary;
	
	/**return 前端页面数据组装VO对象,<b>DictionaryBean</b>*/
	public DictionaryBean getDictionary() {
		return dictionary;
	}

	public void setDictionary(DictionaryBean dictionary) {
		this.dictionary = dictionary;
	}
	
	private DictionaryTypeBean dicTypeBean;
	


	public DictionaryTypeBean getDicTypeBean() {
		return dicTypeBean;
	}

	public void setDicTypeBean(DictionaryTypeBean dicTypeBean) {
		this.dicTypeBean = dicTypeBean;
	}

	//分页对象
	private Page<Dictionary> page = new Page<Dictionary>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<Dictionary> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<Dictionary> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(dictionary==null){
			dictionary = new DictionaryBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public DictionaryBean getModel() {
		return dictionary;
	}
	
	@Override
	protected void prepareModel(){
		if(dictionary!=null&&StringUtils.isNotBlank(dictionary.getDictionaryId())){
			dictionary = (DictionaryBean)dozer.map(dictionaryService.getDictionary(dictionary.getDictionaryId()),DictionaryBean.class);
		}
	}
	

	/**
	 * 删除<b>Dictionary</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到dictionary-list.action
	 */
	@Override
	@Action(value="dictionary-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","dictionary-list?dictionary.dictionaryType.dicTypeId=${dicTypeBean.dicTypeId}"})}
			)
	public String delete(){
		dictionaryService.delete(key);
		return RELOAD;
	}
	
	/**
	 * Dictionary的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到dictionary-list.jsp页面
	 */
	@Override
	@Action(value="dictionary123-list")
	public String list(){
		dictionaryService.list(page,dictionary);
		dicTypeBean=(DictionaryTypeBean)dozer.map(dictionaryTypeService.getDictionaryType(dictionary.getDictionaryType().getDicTypeId()),DictionaryTypeBean.class);
		return SUCCESS;
	}
	/**
	 * Dictionary的新增与编辑的方法执行成功能
	 * 将映射到dictionary-input.jsp
	 */
	@Action(value="dictionary-input")
	public String input(){
		prepareModel();
		if(dicTypeBean!=null&&StringUtils.isNotBlank(dicTypeBean.getDicTypeId())){
			dicTypeBean = (DictionaryTypeBean)dozer.map(dictionaryTypeService.getDictionaryType(dicTypeBean.getDicTypeId()), DictionaryTypeBean.class);
		}
		return INPUT;
	}
	
	/**
	 * save or update Dictionary的方法，将成员property dictionary转成Dictionary实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到dictionary-list.action
	 */
	@Override
	@Action(value="dictionary-save",
			results={@Result(name="reload",type="redirectAction",params={"actionName","dictionary-list?dictionary.dictionaryType.dicTypeId=${dicTypeBean.dicTypeId}"})}
		)
	public String save(){
		Dictionary obj = (Dictionary) dozer.map(dictionary, Dictionary.class);
		if(!StringUtils.isNotBlank(obj.getDictionaryId())){
			obj.setDictionaryId(null);
		}
		obj.setDictionaryType(dicTypeBean);
		dictionaryService.save(obj);
		return RELOAD;
	}
	/**
	 * 异步验证字典KEY是否存在
	 * @return
	 */
	@Action(value="dictionary-validate")
	public String validateDicKey(){
		Dictionary dic = dictionaryService.getDictionaryByDictionaryKey(dictionary.getDictionaryKey(), dicTypeBean.getDicTypeCode());
		if(dic!=null){
			return renderText(IS_EXIST);
		}else{
			return renderText(IS_NOT_EXIST);
		}
	}
}